const http = require('http')
const url = require('url')
const qs = require('querystring')

const server = http.createServer((req, res)=>{
  // console.log('请求已进入')
  let {pathname, query} = url.parse(req.url);
  console.log('pathname:',pathname)
  console.log('query:',query)

  // post
  let arr = []
  req.on('data',(data)=>{
    arr.push(data)
  })
  req.on('end', ()=>{
    // console.log(Buffer.concat(arr).toString())
    let obj = Buffer.concat(arr).toString()
    // console.log('headers',req.headers['content-type'])
    if (req.headers['content-type']=='application/json'){
      // console.log('json数据：',JSON.parse(obj))
      let a = JSON.parse(obj)
      a.add = '老菜鸟'
      res.end(JSON.stringify(a))
    }else if(req.headers['content-type']=='application/x-www-form-urlencoded'){
      // console.log('form表单数据传入',obj)
      let ret = qs.parse(obj)
      res.end(JSON.stringify(ret))
    }
  })
})
server.listen(1234,()=>{
  console.log('http服务开始运行')
})
